import logging

# 创建 logger
logger = logging.getLogger('MyLogger')
logger.setLevel(logging.DEBUG)  # 设置日志级别

# 创建一个 FileHandler 输出日志到文件
file_handler = logging.FileHandler('output_and_error_log.log')
file_handler.setLevel(logging.DEBUG)  # 设置文件的日志级别
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))  # 设置文件日志格式

# 创建一个 StreamHandler 输出日志到控制台
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)  # 设置控制台的日志级别
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))  # 设置控制台日志格式

# 添加这两个 Handler 到 logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
if __name__ == '__main__':
    # 测试日志输出
    logger.debug('This is a debug message')
    logger.info('This is an info message')
    logger.warning('This is a warning message')
    logger.error('This is an error message')
    logger.critical('This is a critical message')
